package com.bjpowernode.validation;

import com.bjpowernode.beans.Stu;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class StuValidation implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return Stu.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Stu temp = (Stu) target;
        if (temp.getId() < 1 || temp.getId() > 1000) {
            errors.rejectValue("id", "stu.id", "默认的错误消息：id超标");
        }

//        if (temp.getName()==null  || temp.getName().trim().isEmpty()) {
//            errors.rejectValue("name", "stu.name", "默认的错误消息：name为空");
//        }


        ValidationUtils.rejectIfEmptyOrWhitespace(errors,"name","stu.name","默认的错误消息：name为空");


        if (temp.getScore() < 0 || temp.getScore() > 100) {
            errors.rejectValue("score", "stu.score", "默认的错误消息：score超标");
        }
    }
}
